www.gusucode.com > VC++ 汽配销售管理系统(Access)源码程序 > VC++ 汽配销售管理系统(Access)源码程序/源码/RxEdit.cpp
//Download by http://www.NewXing.com // RxEdit.cpp : implementation file // #include "stdafx.h" #include "qpglxt.h" #include "RxEdit.h" #include "RxGrid.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif # define EGRID 9999 ///////////////////////////////////////////////////////////////////////////// // RxEdit RxEdit::RxEdit() { this->IsShowing=false; this->Init=false; EnterNumber=0; PopHide=false; } RxEdit::~RxEdit() { m_Grid.DestroyWindow(); } BEGIN_MESSAGE_MAP(RxEdit, CEdit) //{{AFX_MSG_MAP(RxEdit) ON_CONTROL_REFLECT(EN_CHANGE, OnChange) ON_CONTROL_REFLECT(EN_KILLFOCUS, OnKillfocus) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // RxEdit message handlers void RxEdit::ShowGrid() { this->AutoPosition(); m_Grid.ShowWindow(SW_SHOW); this->IsShowing=true; } bool RxEdit::Initialize(CWnd *cParent) { Parent=cParent; try{ m_Grid.Create(WS_BORDER,CRect(0,0,200,200),cParent,EGRID); } catch(...) { return false; } this->ModifyStyle(LVS_EDITLABELS,0L); //标题栏不可编辑 m_Grid.ModifyStyle(0L,LVS_REPORT); m_Grid.ModifyStyle(0L,LVS_SHOWSELALWAYS); //高亮显示被选中项 m_Grid.SetExtendedStyle(LVS_EX_FULLROWSELECT| //允许整行选中 LVS_EX_HEADERDRAGDROP| //允许整列拖动 LVS_EX_GRIDLINES| //画出网格线 LVS_EX_ONECLICKACTIVATE| //单击选中项 LVS_EX_FLATSB); //扁平风格显示滚动条 m_Grid.SetBkColor(RGB(255,255,230)); m_Grid.SetTextBkColor(RGB(255,255,230)); return true; } void RxEdit::SetRecordset(RxRecordset prst) { int cols[255]; this->m_Grid.GetColumnOrderArray(&cols[0]); for(int i=0;i<255;i++) { if(cols[i]==-858993460) break; m_Grid.DeleteColumn(i); } m_Grid.DeleteColumn(0); rst=prst; for(int m=0;m<prst.GetFieldCount();m++) { m_Fields[m]=prst.GetFieldName(m); m_Grid.InsertColumn(m,m_Fields[m]); } this->AddItemValue(prst); } void RxEdit::AutoPosition() { CRect EdtRect,ParentRect,NewRect; this->GetWindowRect(&EdtRect); Parent->GetWindowRect(&ParentRect); //取得 Edit 在 Parent 上的位置 NewRect.top=EdtRect.top-ParentRect.top-23+EdtRect.Height(); NewRect.left=EdtRect.left-ParentRect.left-4; NewRect.right=NewRect.left+GridRect.Width(); NewRect.bottom=NewRect.top+120; //处理横坐标 if(NewRect.left>ParentRect.Width()/2) { NewRect.right=EdtRect.right-ParentRect.left-3; NewRect.left=NewRect.right-GridRect.Width(); if(NewRect.left<20) NewRect.left=20; } else { NewRect.right=NewRect.left+GridRect.Width(); if(NewRect.right>=::GetSystemMetrics(SM_CXSCREEN)-100) NewRect.right=::GetSystemMetrics(SM_CXSCREEN)-100; } Parent->ClientToScreen(&NewRect); m_Grid.MoveWindow(&NewRect); } BOOL RxEdit::PreTranslateMessage(MSG* pMsg) { if(m_bOnly==true) { if(pMsg->message==WM_KEYDOWN&&((pMsg->wParam<48||pMsg->wParam>57)&&pMsg->wParam!=8&&pMsg->wParam!=8&&pMsg->wParam!=38&&pMsg->wParam!=40&&pMsg->wParam!=13&&pMsg->wParam!=VK_ESCAPE)) if((pMsg->wParam<VK_NUMPAD0||pMsg->wParam>VK_NUMPAD9)) { pMsg->wParam=VK_CONTROL ; return true; } } if(pMsg->message==WM_KEYDOWN && pMsg->wParam==VK_ESCAPE) { if(this->IsShowing==true)//截获ESC { this->HideGrid(); pMsg->wParam=VK_CONTROL; } } //截获↓ if(pMsg->message==WM_KEYDOWN && pMsg->wParam==VK_DOWN) { if(this->PopHide==false) { if(this->IsShowing==false) { this->ShowGrid(); this->OnChange(); } } } CString sText; if(this->Init==false) { //设置字体、字号 LOGFONT logfont; logfont.lfCharSet=134; logfont.lfClipPrecision=0; logfont.lfEscapement=0; logfont.lfHeight=-12; logfont.lfItalic=0; logfont.lfOrientation=0; logfont.lfOutPrecision=0; logfont.lfPitchAndFamily=2; logfont.lfQuality=0, logfont.lfStrikeOut=0; logfont.lfUnderline=0; logfont.lfWeight=400; logfont.lfWidth=0; strcpy(logfont.lfFaceName,"宋体"); m_Font.DeleteObject(); m_Font.CreateFontIndirect(&logfont); this->SetFont(&m_Font); this->Init=true; } //如果作为表格的单元格 CString RunTimeClass="CListCtrl"; if(this->IsShowing==true) { if(pMsg->message==WM_KEYDOWN && pMsg->wParam==38) { m_Grid.SetFocus(); this->SetFocus(); pMsg->hwnd=m_Grid.GetSafeHwnd(); pMsg->message=WM_KEYDOWN; pMsg->wParam=38; } if(pMsg->message==WM_KEYDOWN && pMsg->wParam==40) { m_Grid.SetFocus(); this->SetFocus(); pMsg->hwnd=m_Grid.GetSafeHwnd(); pMsg->message=WM_KEYDOWN; pMsg->wParam=40; } if(pMsg->message==WM_KEYDOWN && pMsg->wParam==13) { if(m_Grid.GetSelectionMark()==-1) // return true; { sText=m_Grid.GetItemText(0,m_FieldNameNumber); this->SetWindowText(sText); this->HideGrid(); return true; } sText=m_Grid.GetItemText(m_Grid.GetSelectionMark(),m_FieldNameNumber); this->SetWindowText(sText); this->HideGrid(); } EnterNumber=0; } else { if( this->GetParent()->GetRuntimeClass()->m_lpszClassName==RunTimeClass) { RxGrid* rParent =(RxGrid*)GetParent(); if(pMsg->message==WM_KEYUP && pMsg->wParam==13) pMsg->hwnd=Parent->GetParent()->GetSafeHwnd(); if(pMsg->message==WM_KEYDOWN && pMsg->wParam==13) { EnterNumber=EnterNumber+1; if(EnterNumber>1) //用户敲两次回车 goto end; this->GetWindowText(sText); if(sText.IsEmpty()) return false; rParent->MoveNextItem(); pMsg->hwnd=Parent->GetParent()->GetSafeHwnd(); } } end: if(pMsg->message==WM_KEYDOWN && pMsg->wParam==13) pMsg->wParam=9; } return CEdit::PreTranslateMessage(pMsg); } void RxEdit::HideGrid() { this->m_Grid.ShowWindow(SW_HIDE); this->IsShowing=false; } void RxEdit::SetSelectField(CString sFieldName) { this->m_FieldName=sFieldName; for(int m=0;m<255;m++) { if(sFieldName==this->m_Fields[m]) { m_FieldNameNumber=m; break; } } } void RxEdit::OnChange() { if(this->PopHide==true) return ; if(this->m_FieldName.IsEmpty()) return; CString sSQL,sText,sRecordset; RxRecordset OnCrst; sRecordset=rst.GetSQL(); if(sRecordset.IsEmpty()) return; BYTE chr; this->GetWindowText(sText); if(sText.IsEmpty()==false) chr=sText.GetAt(0); else goto Next; if(this->StartUpAssciation==true)//启动联系输入 { if(chr>=97 &&chr<=122) //小写字母 sSQL.Format("SELECT * From %s WHERE 简称 LIKE'%s%%'",sRecordset,sText); else goto Next; } else Next: sSQL.Format("SELECT * From %s WHERE %s LIKE'%s%%'",sRecordset,this->m_FieldName,sText); OnCrst.Open(sSQL,adCmdText); if (OnCrst.GetRecordCount()<=0) return; if(this->IsShowing==false) this->ShowGrid(); this->AddItemValue(OnCrst); // this->SetSel(sText.GetLength(),0); m_Grid.SetParent(Parent->GetParent()); } void RxEdit::AddItemValue(RxRecordset prst) { CString sValue; int ColumnWidth=0; m_Grid.DeleteAllItems(); for(int m=0;m<prst.GetRecordCount();m++) { m_Grid.InsertItem(m,""); } for( m=0;m<prst.GetRecordCount();m++) { prst.Move(m); for(int n=0;n<prst.GetFieldCount();n++) { sValue=prst.GetFieldValue(prst.GetFieldName(n)); m_Grid.SetItemText(m,n,sValue); } } for(m=0;m<prst.GetFieldCount();m++) { m_Grid.SetColumnWidth(m,LVSCW_AUTOSIZE); } for(m=0;m<prst.GetFieldCount();m++) { m_Grid.SetColumnWidth(m,m_Grid.GetColumnWidth(m)+20); ColumnWidth=ColumnWidth+m_Grid.GetColumnWidth(m); } CRect rect; m_Grid.GetClientRect(&rect); rect.right=rect.left+ColumnWidth; GridRect=rect; } void RxEdit::OnKillfocus() { CString RunTimeClass="CListCtrl"; if(this->GetParent()->GetRuntimeClass()->m_lpszClassName==RunTimeClass) { RxGrid* Parent =(RxGrid*)GetParent(); if(Parent) Parent->EndEdit(); } EnterNumber=0; } void RxEdit::ClearAll() { try{ m_Grid.DeleteAllItems(); } catch(...) { return; } for(int m=0;m<rst.GetFieldCount();m++) { try{ m_Grid.DeleteColumn(0); } catch(...) { break; } } } void RxEdit::NumberOnly(bool bOnly) { m_bOnly=bOnly; }